package math;

/**
 * Created by zhoub on 2020/3/24.
 */
public class Line {
    public static double [] getLinePara(Double [] [] points ) {
        double dbRt [] =new double [2];
        double dbXSum=0;
        for(int i=0;i<points[0].length;i++) {
            dbXSum=dbXSum+points[0][i];
        }
        double dbXAvg=dbXSum/points[0].length;
        double dbWHeadVal=0;
        for(int i=0;i<points[0].length;i++) {
            dbWHeadVal=dbWHeadVal+(points[0][i]-dbXAvg)*points[1][i];
        }
        double dbWDown=0;
        double dbWDownP=0;
        double dbWDownN=0;
        dbXSum=0;
        for(int i=0;i<points[0].length;i++) {
            dbWDownP=dbWDownP+points[0][i]*points[0][i];
            dbXSum=dbXSum+points[0][i];
        }
        dbWDown=dbWDownP-(dbXSum*dbXSum/points[0].length);
        double dbW=dbWHeadVal/dbWDown;
        dbRt[0]=dbW;
        double dbBSum=0;
        for(int i=0;i<points[0].length;i++) {
            dbBSum=dbBSum+(points[1][i]-dbW*points[0][i]);
        }
        double dbB=dbBSum/points[0].length;
        dbRt[1]=dbB;
        return dbRt;
    }




    public static void main(String[] args) {
        Double [][] arrPoints= {{0.29,86.59,69.38,51.99,34.64,17.37},{0.00,99.00,79.00,59.00,39.00,19.00}};
        /** 预测 w为1 b为0*/
        System.out.println(getLinePara(arrPoints)[0]);
        System.out.println(getLinePara(arrPoints)[1]);
        /**测试结果与预测相同*/
    }

}
